package com.corpgovernment.organization.service.impl;

import com.corpgovernment.api.organization.model.expressaddress.UserAddressVo;
import com.corpgovernment.common.common.CorpBusinessException;
import com.corpgovernment.organization.constant.OrganizationResponseCodeEnum;
import com.corpgovernment.organization.entity.db.MbExpressAddress;
import com.corpgovernment.organization.mapper.MbExpressAddressMapper;
import com.corpgovernment.organization.service.OrganizationBaseService;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import com.ctrip.corp.obt.shard.context.PageContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ExpressAddressServiceImpl extends OrganizationBaseService {

    @Autowired
    private MbExpressAddressMapper expressAddressMapper;


    /**
     * 插入一条快递地址
     *
     * @param model
     */
    public Boolean insertExpressAddress(MbExpressAddress model) {
        int count = expressAddressMapper.insertSelective(model);
//        insertOperateLog(model);
        return count > 0;
    }


    public int delByOrgIds(List<String> orgIds){
        Example example = new Example(MbExpressAddress.class);
        example.createCriteria().andIn("orgId", orgIds);
        MbExpressAddress address = new MbExpressAddress();
        address.setIsDeleted(true);
        return expressAddressMapper.updateByExample(address, example);
    }

    /**
     * 查询多条快递地址
     *
     * @param orgIds
     * @return
     */
    List<MbExpressAddress> findListByOrgIds(List<String> orgIds) {

        List<MbExpressAddress> list = expressAddressMapper.findListByOrgIds(orgIds);
        return list;
    }

    public List<MbExpressAddress> listByUid(String uid, String selectVal, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            PageContext.startPage(pageNum, pageSize);
        }
        return expressAddressMapper.listByUid(uid, selectVal);

    }

    List<MbExpressAddress> listByOrgIdAndSelectVal(String orgId, String uid, String selectVal, Integer isCorporation, Integer pageNum, Integer pageSize) {
        PageContext.startPage(pageNum, pageSize);
        return expressAddressMapper.listByOrgIdAndSelectVal(orgId, uid, selectVal, isCorporation);
    }

    public Boolean updateUserAddress(MbExpressAddress model) {

        if (model == null || model.getId() == null) {
            return false;
        }

        return expressAddressMapper.updateByPrimaryKeySelective(model) > 0;
    }

    public Boolean deleteUserAddress(UserAddressVo userAddressVo) {
        Long id = userAddressVo.getId();
        if (id == null) {
            return false;
        }
        if (expressAddressMapper.listByUid(userAddressVo.getUid(), null).stream()
            .noneMatch(a -> a.getId().equals(id))) {
            log.info("该数据[{}]不属于当前用户[{}]", id, userAddressVo.getUid());
            throw new CorpBusinessException(OrganizationResponseCodeEnum.DATA_OPERATION_NO_PERMISSION);
        }
        MbExpressAddress deleteParam = new MbExpressAddress();
        deleteParam.setId(id);
        deleteParam.setIsDeleted(true);
        return expressAddressMapper.updateByPrimaryKeySelective(deleteParam) > 0;
    }

    public MbExpressAddress findUserRecentUseAddress(String uid) {
        return expressAddressMapper.findUserRecentUseAddress(uid);
    }

    public List<MbExpressAddress> listByOrgId(String orgId) {
        return expressAddressMapper.listByOrgId(orgId);
    }

    public List<MbExpressAddress> listByOrgIdAndDefaultAddress(String orgId, Boolean defaultAddress) {

        if (StringUtils.isEmpty(orgId)) {
            return null;
        }

        MbExpressAddress selectParam = new MbExpressAddress();
        selectParam.setOrgId(orgId);
        selectParam.setIsDeleted(false);
        selectParam.setIsCorporation(false);
        selectParam.setIsDefault(defaultAddress);
        return expressAddressMapper.select(selectParam);
    }

    public void insertBatch(List<MbExpressAddress> list) {
        if (CollectionUtils.isEmpty(list)){
            return;
        }
        expressAddressMapper.insertSelectiveBatch(list);
    }

    public void updateByOrgId(String orgId) {

        if (StringUtils.isEmpty(orgId)) {
            return;
        }

        MbExpressAddress selectParam = new MbExpressAddress();
        selectParam.setOrgId(orgId);
        selectParam.setIsDeleted(false);
        selectParam.setIsDefault(true);
        List<MbExpressAddress> list = expressAddressMapper.select(selectParam);
        if (CollectionUtils.isEmpty(list)) {
            return;
        }

        List<MbExpressAddress> updateList = list.stream().map(a -> {
            MbExpressAddress temp = new MbExpressAddress();
            temp.setId(a.getId());
            temp.setIsDefault(false);
            return temp;
        }).collect(Collectors.toList());
        updateBatch(updateList);
    }

    public void updateBatch(List<MbExpressAddress> list) {

        list = Optional.ofNullable(list).orElse(new ArrayList<>()).stream().filter(a -> a != null && a.getId() != null).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        expressAddressMapper.updateSelectiveBatch(list);
    }

    /**
     * 删除组织ID下的所有配送地址
     *
     * @param orgId
     * @return
     */
    public boolean deleteByOrgId(String orgId) {
        if (StringUtils.isEmpty(orgId)) {
            return false;
        }

        return expressAddressMapper.deleteByOrgId(orgId) > 0;
    }
}
